let form
let table
let iframe

window.addEventListener("load", function () {
    $("#list")[0].onload = function () {
        iframe = $("#list")[0].contentWindow
        table = iframe.document.querySelector("#list")
        table.checkboxes = table.querySelectorAll("input")
        table.selects = table.querySelectorAll("a")
        table.status = table.querySelectorAll(".status span")

        form = $(iframe.document.forms[0])

        if (!OPS.isHide(OPS.delete)) {
            OPS.delete.button.onclick()
        }
        if (!OPS.isHide(OPS.modify)) {
            OPS.modify.button.onclick()
        }
    }
})

// 查询功能
document.body.onload = function () {
    document.forms['father'].submit()
}

// 删除功能
function sendData() {
    $.ajax({
        url: window.location.origin + '/red_war_exploded/deleteHouse',
        type: 'POST',
        dataType: 'text',
        data: form.serialize(),
        success: function () {
            alert("删除成功")
            iframe.location.reload();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("删除失败，状态码：" + XMLHttpRequest.status)
        }
    });
}

//------------------ 按钮面板切换 ------------------
// 操作按钮组
const OPS = {
    panel: $("#option_group")[0],
    delete: {
        panel: $("#delete_group")[0],
        button: $("#option_group [fun='delete']")[0],
        ok: $("#delete_group [fun='ok']")[0],
        cancel: $("#delete_group [fun='cancel']")[0]
    },
    modify: {
        panel: $("#modify_group")[0],
        button: $("#option_group [fun='modify']")[0],
        cancel: $("#modify_group [fun='cancel']")[0]
    },
    hide: function (option) {
        option.panel.style.display = "none"
    },
    show: function (option) {
        option.panel.style.display = "block"
    },
    isHide: function (option) {
        return option.panel.style.display === "none"
    }
}

OPS.hide(OPS.delete)
OPS.hide(OPS.modify)

OPS.modify.button.onclick = function () {
    OPS.hide(OPS)
    OPS.show(OPS.modify)

    let selects = table.selects

    for (let i = 0; i < selects.length; i++) {
        selects[i].style.display = "inline-block"

        if (table.status[i].innerText !== "未出租") {
            selects[i].href = '#'
            selects[i].target = ''
            selects[i].style.background = 'grey'
        }
    }



}
OPS.modify.cancel.onclick = function () {
    OPS.hide(OPS.modify)
    OPS.show(OPS)

    let selects = table.selects

    for (let i = 0; i < selects.length; i++) {
        selects[i].style.display = "none"
    }
}


// 删
OPS.delete.button.onclick = function () {
    OPS.hide(OPS)
    OPS.show(OPS.delete)

    let checkboxes = table.checkboxes

    for (let i = 0; i < checkboxes.length; i++) {
        checkboxes[i].className = ""

        if (table.status[i].innerText !== "未出租") {
            checkboxes[i].disabled = true
        }
    }
}

OPS.delete.cancel.onclick = function () {
    OPS.hide(OPS.delete)
    OPS.show(OPS)

    let checkboxes = table.checkboxes

    for (let i = 0; i < checkboxes.length; i++) {
        checkboxes[i].className = "hidden"
    }
}

